#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask import Blueprint, Flask, jsonify

from flask_docs import ApiDoc

app = Flask(__name__)

# Using CDN
# app.config["API_DOC_CDN"] = True

# Disable document pages
# app.config["API_DOC_ENABLE"] = False

# Methods allowed to be displayed
# app.config["API_DOC_METHODS_LIST"] = ["GET", "POST", "PUT", "DELETE", "PATCH"]

# Custom url_prefix
# app.config["API_DOC_URL_PREFIX"] = "/docs/api"

# Api Document needs to be displayed
app.config["API_DOC_MEMBER"] = ["web", "platform"]

ApiDoc(app, title="Sample App", version="1.0.0")

web = Blueprint("web", __name__)
# platform = Blueprint("platform", __name__)


@web.route("/")
def test_api():
    """测试连通接口
    @@@
    ### args
    None
    ### request
    ```
        http://127.0.0.1:5000/web/
    ```
    ### return
    ```text
    "hi! 访问正确!"
    ```
    @@@
    """
    return jsonify({"api": "/"})


@web.route("/init_configure")
def init_configure():
    """ 资产(inventory) 初始化配置
        @@@
        ### args
        |  args  | nullable | request type | type |  remarks     |
        |--------|----------|--------------|------|--------------|
        |  ip    |  false    |    query     |  str | 资产的IP    |
        |  username  |  false   |    query     |  str | 资产登录的用户名 |
        |  password  |  false   |    query     |  str | 资产登录的密码 |
        ### request
        ```
        http://127.0.0.1:5000/web/init_configure?ip=xxx&username=xxx&password=xxx
        ```
        ### return
        ```json
        {"code": xxxx, "data": xxxx}
        ```
        @@@
        """
    return jsonify({"api":"/init_configure"})


@web.route("/safe_detect_configure")
def safe_detect_configure():
    """ 资产检测初始化设置
            @@@
            ### args
            |  args  | nullable | request type | type |  remarks     |
            |--------|----------|--------------|------|--------------|
            |  time    |  false    |    query     |  str | 检测的时间，e.g 22:30    |
            |  id  |  false   |    query     |  int | 资产的编号 |
            ### request
            ```
            http://127.0.0.1:5000/web/safe_detect_configure?time=xxx&id=xxx
            ```
            ### return
            ```json
            {"code": xxxx, "data": xxxx}
            ```
            @@@
    """
    return jsonify({"api": "/safe_detect_configure"})


@web.route("/safe_detect_immediately")
def safe_detect_immediately():
    """ 立即执行资产的安全检测(漏洞扫描)
               @@@
               ### args
               |  args  | nullable | request type | type |  remarks     |
               |--------|----------|--------------|------|--------------|
               |  id  |  false   |    query     |  int | 资产的编号 |
               ### request
               ```
               http://127.0.0.1:5000/web/safe_detect_immediately?id=xxx
               ```
               ### return
               ```json
               {"code": xxxx, "data": xxxx}
               ```
               @@@
    """
    return jsonify({"api": "/safe_detect_immediately"})


@web.route("/hostname_detect")
def hostname_detect():
    """ 主机名检测,获取Hostname
           @@@
           ### args
           |  args  | nullable | request type | type |  remarks     |
           |--------|----------|--------------|------|--------------|
           |  ip    |  false    |    query     |  str | 资产的IP    |
           |  username  |  false   |    query     |  str | 资产登录的用户名 |
           |  password  |  false   |    query     |  str | 资产登录的密码 |
           |  port  |  false   |    query     |  int | 资产登录的端口 |
           ### request
           ```
           http://127.0.0.1:5000/web/hostname_detect?ip=xxx&username=xxx&password=xxx
           ```
           ### return
           ```json
           {"code": xxxx, "data": xxxx} 如果需要修改hostname返回yes,不需要返回no
           ```
           @@@
           """
    return jsonify({"api": "hostname_detect"})


@web.route("/usability_detect")
def usability_detect():
    """ 可用性探测
           @@@
           ### args
           |  args  | nullable | request type | type |  remarks     |
           |--------|----------|--------------|------|--------------|
           |  ip    |  false    |    query     |  str | 资产的IP    |
           |  username  |  false   |    query     |  str | 资产登录的用户名 |
           |  password  |  false   |    query     |  str | 资产登录的密码 |
           |  port  |  false   |    query     |  int | 资产登录的端口 |
           ### request
           ```
           http://127.0.0.1:5000/web/usability_detect?ip=xxx&username=xxx&password=xxx
           ```
           ### return
           ```json
           {"code": xxxx, "data": xxxx} 错误会返回400010
           ```
           @@@
           """
    return jsonify({"api": "usability_detect"})


@web.route("/hostname_set")
def hostname_set():
    """ 主机名检测,获取Hostname
               @@@
               ### args
               |  args  | nullable | request type | type |  remarks     |
               |--------|----------|--------------|------|--------------|
               |  ip    |  false    |    query     |  str | 资产的IP    |
               |  username  |  false   |    query     |  str | 资产登录的用户名 |
               |  password  |  false   |    query     |  str | 资产登录的密码 |
               |  port  |  false   |    query     |  str | 资产的端口 |
               |  hostname  |  false   |    query     |  str | 要设置的主机名(主机名) |
               ### request
               ```
               http://127.0.0.1:5000/web/hostname_detect?ip=xxx&username=xxx&password=xxx
               ```
               ### return
               ```json
               {"code": xxxx, "data": xxxx}
               ```
               @@@
               """
    return jsonify({"api": "hostname_set"})


@web.route("/remove_safe_detect")
def remove_safe_detect():
    """ 立即执行资产的安全检测(漏洞扫描)
                   @@@
                   ### args
                   |  args  | nullable | request type | type |  remarks     |
                   |--------|----------|--------------|------|--------------|
                   |  id  |  false   |    query     |  int | 资产的编号 |
                   ### request
                   ```
                   http://127.0.0.1:5000/web/remove_safe_detect?id=xxx
                   ```
                   ### return
                   ```json
                   {"code": xxxx, "data": xxxx}
                   ```
                   @@@
        """
    return jsonify({"api": "remove_safe_detect"})


@web.route("/show_jobs")
def show_jobs():
    """ 查看所有任务

    :return:
    """
    return jsonify({"api": "show_jobs"})


@web.route("/modify_strategy")
def modify_strategy():
    """ 修改补丁分发策略
            @@@
            ### args
            |  args  | nullable | request type | type |  remarks     |
            |--------|----------|--------------|------|--------------|
            |  s_id    |  false    |    query     |  int | 策略编号    |
            ### request
            ```
            http://127.0.0.1:5000/web/modify_strategy?s_id=xxxx
            ```
            ### return
            ```json
            {"code": xxxx, "data": xxxx}
            ```
            @@@
            """
    return jsonify({"api": "modify_strategy"})


@web.route("/add_strategy")
def add_strategy():
    """ 添加补丁分发策略
            @@@
            ### args
            |  args  | nullable | request type | type |  remarks     |
            |--------|----------|--------------|------|--------------|
            |  s_id    |  false    |    query     |  int | 策略编号    |
            ### request
            ```
            http://127.0.0.1:5000/web/add_strategy?s_id=xxxx
            ```
            ### return
            ```json
            {"code": xxxx, "data": xxxx}
            ```
            @@@
            """
    return jsonify({"api": "add_strategy"})


@web.route("/remove_strategy")
def remove_strategy():
    """ 删除补丁分发策略
            @@@
            ### args
            |  args  | nullable | request type | type |  remarks     |
            |--------|----------|--------------|------|--------------|
            |  s_id    |  false    |    query     |  int | 策略编号    |
            ### request
            ```
            http://127.0.0.1:5000/web/remove_strategy?s_id=xxxx
            ```
            ### return
            ```json
            {"code": xxxx, "data": xxxx}
            ```
            @@@
            """
    return jsonify({"api": "remove_strategy"})

# @web.route("/add_data", methods=["POST"])
# def add_data():
#     """Add some data
#     @@@
#     ### args
#     |  args | nullable | request type | type |  remarks |
#     |-------|----------|--------------|------|----------|
#     | title |  false   |    body      | str  | blog title    |
#     | name  |  false   |    body      | str  | person's name |
#     ### request
#     ```json
#     {"title": "xxx", "name": "xxx"}
#     ```
#     ### return
#     ```json
#     {"code": xxxx, "msg": "xxx", "data": null}
#     ```
#     @@@
#     """
#     return jsonify({"api": "add data"})


#
# @platform.route("/get_something", methods=["GET"])
# def get_something():
#     """Get some data
#     @@@
#     ### request example
#     ```python
#     import requests
#     url="http://127.0.0.1:5000/platform/get_something"
#     try:
#         print(requests.get(url).text)
#     except:
#         pass
#     ```
#     ### return
#     ```json
#     {"code": xxxx, "msg": "xxx", "data": null}
#     ```
#     @@@
#     """
#
#     return jsonify({"platform": "get something"})

#
# @web.route("/delete_data", methods=["GET"])
# def delete_data():
#     """(Mock 接口)Delete some data
#     @@@
#     ### args
#     |  args  | nullable | request type | type |  remarks     |
#     |--------|----------|--------------|------|--------------|
#     |  id    |  true    |    query     |  str | blog id    |
#     |  name  |  false   |    query     |  str | person's name |
#     ### request
#     ```
#     http://127.0.0.1:5000/api/delete_data?name=xxx
#     ```
#     ### return
#     ```json
#     {"code": xxxx, "msg": "xxx", "data": null}
#     ```
#     @@@
#     """
#
#     return jsonify({"api": "delete data"})


app.register_blueprint(web, url_prefix="/web")
# app.register_blueprint(platform, url_prefix="/platform")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)